package com.ayf.payment.game.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.ayf.payment.game.api.dto.MerchantPermissionDTO;
import com.ayf.payment.game.api.dto.MerchantPermissionVO;
import com.ayf.payment.game.api.entity.MerchantPermission;
import com.ayf.payment.game.api.entity.MerchantSon;
import com.ayf.payment.game.api.mapper.MerchantPermissionMapper;
import com.ayf.payment.game.api.service.MerchantPermissionService;
import com.ayf.payment.game.api.service.MerchantSonService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MerchantPermissionServiceImpl implements MerchantPermissionService {

    @Resource
    private MerchantPermissionMapper merchantPermissionMapper;
    @Resource
    private MerchantSonService merchantSonService;

    @Override
    public MerchantPermissionDTO adminList(String merchantType) {
        MerchantPermissionDTO dto = new MerchantPermissionDTO();
        Map<String, Object> map = new HashMap<>();

        List<MerchantPermissionVO> permissionList1 = merchantPermissionMapper.permissionBySid(0, 1);
        if (StringUtils.isNotBlank(merchantType)) {
            if (!"PROXY".equals(merchantType)) {
                for (MerchantPermissionVO permission : permissionList1) {
                    if (permission.getId() == 16) {
                        permissionList1.remove(permission);
                        break;
                    }
                }
            }
        }
        dto.setList1(permissionList1);

        List<MerchantPermissionVO> permissionList2 = merchantPermissionMapper.permissionBySid(1, 1);
        if (permissionList2 != null && permissionList2.size() == 2) {
            map.put("DaiFu", permissionList2.get(0));
            map.put("TiXian", permissionList2.get(1));
        }

        List<MerchantPermissionVO> permissionList3 = merchantPermissionMapper.permissionBySid(11, 1);
        dto.setList3(permissionList3);

        List<MerchantPermissionVO> permissionList4 = merchantPermissionMapper.permissionBySid(15, 1);
        dto.setList4(permissionList4);
        for (MerchantPermissionVO permission : permissionList4) {
            if (permission.getId() == 119) {
                map.put("TiXianJiLu", permission);
            }
            if (permission.getId() == 120) {
                map.put("DaiFuJiLu", permission);
            }
        }

        List<MerchantPermissionVO> permissionList5 = merchantPermissionMapper.permissionBySid(16, 1);
        dto.setList5(permissionList5);

        dto.setMap2(map);

        List<MerchantPermissionVO> permissionList6 = merchantPermissionMapper.permissionBySid(19,1);
        permissionList6.forEach(item->{
            if (1906==item.getId() || 1908 == item.getId()){
                item.setSon(merchantPermissionMapper.permissionBySid(item.getId(),1));
            }
        });
        dto.setList6(permissionList6);

        return dto;
    }

    @Override
    public List<MerchantPermission> list(Integer permissionType) {
        List<MerchantPermission> merchantPermissionList = merchantPermissionMapper.list(permissionType);
        merchantPermissionList.forEach(item->{
            if (19==item.getId()){
                item.getSon().forEach(item2->{
                    if (1906==item2.getId()||1908==item2.getId()){
                        item2.setSon(BeanConvertUtils.convert(merchantPermissionMapper.permissionBySid(item2.getId(),1),MerchantPermission.class));
                    }
                });
            }
        });
        return merchantPermissionList;
    }

    @Override
    public MerchantPermissionDTO sonPermissionList(Integer roleId) {

        MerchantPermissionDTO dto = new MerchantPermissionDTO();
        Map<String, Object> map = new HashMap<>();
        List<MerchantPermissionVO> permissionList1 = new ArrayList<>();
        List<MerchantPermissionVO> permissionList3 = new ArrayList<>();
        List<MerchantPermissionVO> permissionList4 = new ArrayList<>();
        List<MerchantPermissionVO> permissionList5 = new ArrayList<>();
        List<MerchantPermissionVO> permissionList6 = new ArrayList<>();

        List<MerchantPermissionVO> permissionList = merchantPermissionMapper.roleList(roleId);

        for (MerchantPermissionVO permission : permissionList) {
            if (permission.getId() == 1001) {
                map.put("DaiFu", permission);
            } else if (permission.getId() == 1003) {
                map.put("TiXian", permission);
            } else if (permission.getId() == 119) {
                map.put("TiXianJiLu", permission);
            } else if (permission.getId() == 120) {
                map.put("DaiFuJiLu", permission);
            }

            if (permission.getsId() == 0) {
                permissionList1.add(permission);
            } else if (permission.getsId() == 11) {
                permissionList3.add(permission);
            } else if (permission.getsId() == 15) {
                permissionList4.add(permission);
            } else if (permission.getsId() == 16) {
                permissionList5.add(permission);
            }else if (permission.getsId()==19){
                if (1906==permission.getId() || 1908 == permission.getId()){
                    List<MerchantPermissionVO> permissionList7 = new ArrayList<>();
                    permissionList.forEach(item2->{
                        if (1906==item2.getsId()|| 1908 ==item2.getsId()){
                            permissionList7.add(item2);
                        }
                    });
                    permission.setSon(permissionList7);
                }
                permissionList6.add(permission);
            }
        }

        dto.setList1(permissionList1);
        dto.setMap2(map);
        dto.setList3(permissionList3);
        dto.setList4(permissionList4);
        dto.setList5(permissionList5);
        dto.setList6(permissionList6);
        return dto;
    }

    @Override
    public List<MerchantPermissionVO> appSonPermissionList(Integer id, Integer loginId, Integer sId) {
        List<MerchantPermissionVO> list;
        if (loginId.equals(id)) {
            list = merchantPermissionMapper.permissionBySid(sId, 2);
            if (CollUtil.isNotEmpty(list)) {
                list.forEach(dto -> {
                    List<MerchantPermissionVO> list2 = merchantPermissionMapper.permissionBySid(dto.getId(), 2);
                    dto.setSon(list2);
                });
            }
        } else {
            MerchantSon merchantSon = merchantSonService.selectInfo(loginId);
            list = merchantPermissionMapper.appRoleList(merchantSon.getRoleId(), sId);
            if (CollUtil.isNotEmpty(list)) {
                list.forEach(dto -> {
                    List<MerchantPermissionVO> list2 = merchantPermissionMapper.appRoleList(merchantSon.getRoleId(), dto.getId());
                    dto.setSon(list2);
                });
            }
        }
        return list;
    }
}
